package bb.lanxing.fragment.routebook;

import androidx.annotation.NonNull;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import bb.lanxing.model.database.RouteBook;
import bb.lanxing.network.BiCiCallback;
import bb.lanxing.network.MyHttpClient;

public class NearByRouteBookListFragment extends RouteBookListFragmentNew {

    @Override
    public void requestData() {
        requestNearbyLushuList(true);
    }

    public void requestNearbyLushuList(final boolean bPullDown) {
        if (bPullDown) {
            resetPage();
        }
        MyHttpClient.requestNearbyLushuList(new BiCiCallback() {
            @Override
            public void onResponseString(@NonNull String strResponse) {
                ArrayList<RouteBook> arrayList = new ArrayList<>();
                try {
                    JSONObject obj = new JSONObject(strResponse);
                    JSONArray jSONArray = obj.getJSONArray("rows");
                    if (jSONArray.length() < getPageSize()) {
                        adapter.setLoadMoreEnabled(false);
                    } else {
                        updatePage();
                        adapter.setLoadMoreEnabled(true);
                    }
                    for (int i = 0; i < jSONArray.length(); i++) {
                        arrayList.add(RouteBook.parseLushuByServer(jSONArray.getJSONObject(i), true));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                refreshUIWithServer(arrayList, bPullDown);
            }

            @Override
            public void onResponseFail(String errMsg) {
                super.onResponseFail(errMsg);
                refreshComplete();
            }
        }, this.getPage(), getPageSize());
    }
    @Override
    protected void loadMoreEvent() {
        requestNearbyLushuList(false);
    }
}